package com.client.view;

import com.client.controller.ClientSocketThreadManager;
//import com.client.controller.screenManager;
import com.client.controller.screenTest;
import com.common.Message;
import com.common.MessageType;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
import java.util.concurrent.TimeUnit;

/**
 * @author lxl
 * @date 2020/12/26 - 9:38
 */
/*
与好友聊天的界面
客户端要处于读取的状态,把他做成线程
 */
public class ChatFrame extends JFrame implements ActionListener, KeyListener {
    //    public static void main(String[] args) {
//        new QqChat("1","2");
//    }
    JFrame parent;
    JTextArea jta;      //文本窗口
    JScrollPane jsp;
    JTextField jtf;
    JButton jb1;        //文本消息按钮
    JButton jb2;        //文件发送按钮
    JButton jb3;        //聊天记录按钮
    JButton jb4;        //窗口抖动按钮
    JButton jb5;        //窗口截屏按钮
    JPanel jp;
    String ownId;
    String friendId;
    public ChatFrame(JFrame parent, String ownId, String friendId){
        this.parent=parent;
        this.ownId=ownId;
        this.friendId=friendId;

        jta = new JTextArea();
        jta.setEditable(false);
        jsp = new JScrollPane(jta);
        jtf = new JTextField(10);
        jtf.addKeyListener(this);
        jb1 = new JButton("发送");
        jb1.addActionListener(this);
        jb2 = new JButton("发送文件");
        jb2.addActionListener(this);
        jb3 = new JButton("聊天记录");
        jb3.addActionListener(this);
        jb4 = new JButton("发送抖动");
        jb4.addActionListener(this);
        jb5=  new JButton("截图");
        jb5.addActionListener(this);
        jp = new JPanel();
        jp.add(jb2);
        jp.add(jtf);
        jp.add(jb1);
        jp.add(jb3);
        jp.add(jb4);
        jp.add(jb5);
        this.setTitle(ownId+" 正在和 "+friendId+" 聊天");
        this.add(jsp);
        this.add(jp,"South");
        this.setSize(800,400);
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);
        this.setLocationRelativeTo(parent);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()== jb1){
            sendMessage();
        }else if(e.getSource()==jb2){
            JFileChooser jf = new JFileChooser();
            jf.setFileSelectionMode(JFileChooser.FILES_ONLY);
            jf.setCurrentDirectory(new File("."));
            int result=jf.showOpenDialog(this);
            if(result==JFileChooser.APPROVE_OPTION){
                Message message = new Message();
                if(friendId.equals("All")){//以All结尾代表群发
                    message.setMesType(MessageType.message_fileToAll);
                }else{
                    message.setMesType(MessageType.message_file);
                }
                File file = jf.getSelectedFile();
                message.fileName=file.getName();
                message.data= new byte[(int) file.length()];
                try {
                    FileInputStream fis = new FileInputStream(file);
                    fis.read(message.data);
                    fis.close();
                } catch (FileNotFoundException fileNotFoundException) {
                    fileNotFoundException.printStackTrace();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                message.setSender(ownId);
                message.setGetter(friendId);
                message.setText("一个文件");
                jta.append("("+message.getSendTime()+")"+ownId+" 刚才给 "+friendId+" 发送了一个文件"+System.lineSeparator());
                try {
                    OutputStream os = ClientSocketThreadManager.getClientConServerThread(ownId).getS().getOutputStream();
                    ObjectOutputStream oos= new ObjectOutputStream(os);
                    oos.writeObject(message);
                }catch (Exception arg0){
                    arg0.printStackTrace();
                }
            }

        }else if(e.getSource()==jb3){
            try {
                new RecordFrame(parent,ownId,friendId);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }else if (e.getSource() == jb4) {
            Message message = new Message();
            if (friendId.equals("All"))
                message.setMesType(MessageType.message_shakeWindowToALL); //抖动窗口类型数据
            else
                message.setMesType(MessageType.message_shakeWindow);
            message.setSender(ownId);                                 //发送者
            message.setGetter(friendId);                              //接收者
            message.setText(ownId + "发送了一个窗口抖动!");
            try {
                OutputStream os = ClientSocketThreadManager.getClientConServerThread(ownId).getS().getOutputStream();
                ObjectOutputStream oos= new ObjectOutputStream(os);
                oos.writeObject(message);
                System.out.println("你发送了一个抖动窗口");
            }catch (Exception arg0){
                arg0.printStackTrace();
            }
        }else if(e.getSource()==jb5){
            Message message=new Message();
            if(friendId.equals("All")){//以All结尾代表群发
                message.setMesType(MessageType.message_fileToAll);
            }else{
                message.setMesType(MessageType.message_file);
            }
            File file=new File("d:\\screenDir");
            if(!file.exists()){
                file.mkdirs();
            }
            screenTest st=new screenTest("d:\\screenDir\\","png");
            //完成截图并将截图存放于文件夹下
            int getNum=st.snapShot();       //同时获得我截取图片的数字

            //发送图片
            String path="d:\\screenDir\\"+String.valueOf(getNum)+".png";
            System.out.println(path);
            File photo=new File(path);

            message.fileName=photo.getName();
            message.data=new byte[(int) photo.length()];

            try {
                FileInputStream fis = new FileInputStream(photo);
                fis.read(message.data);
                fis.close();
            } catch (FileNotFoundException fileNotFoundException) {
                fileNotFoundException.printStackTrace();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }

            message.setSender(ownId);
            message.setGetter(friendId);
            message.setText("一个文件");
            jta.append("("+message.getSendTime()+")"+ownId+" 刚才给 "+friendId+" 发送了截图"+System.lineSeparator());
            try {
                OutputStream os = ClientSocketThreadManager.getClientConServerThread(ownId).getS().getOutputStream();
                ObjectOutputStream oos= new ObjectOutputStream(os);
                oos.writeObject(message);
            }catch (Exception arg0){
                arg0.printStackTrace();
            }

        }
    }

    //窗口抖动
    public void ShakingWindow() {
        this.setVisible(true);
        Point p = this.getLocationOnScreen(); //获取原始位置
        int times = 0;
        while (times < 2) {
            try {
                //进行窗口抖动
                this.setLocation((int)p.getX(), (int)p.getY() + 75);
                Thread.sleep(130);
                this.setLocation((int)p.getX(), (int)p.getY() - 75);
                Thread.sleep(130);
                this.setLocation((int)p.getX() + 75, (int)p.getY());
                Thread.sleep(130);
                this.setLocation((int)p.getX() - 75, (int)p.getY());
                Thread.sleep(130);
                this.setLocation((int)p.getX() - 75, (int)p.getY() - 75);
                Thread.sleep(130);
                this.setLocation((int)p.getX() + 75, (int)p.getY() + 75);
                Thread.sleep(130);
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }
            this.setLocation(p); //恢复位置
            times++;
        }
    }

    public void sendMessage(){
        //点击了发送按钮
        Message message = new Message();
        message.setSender(ownId);
        message.setGetter(friendId);
        if(friendId.equals("All")){//以All结尾代表群发
            message.setMesType(MessageType.message_commonToAll);
        }else{
            message.setMesType(MessageType.message_common);
        }
        message.setText(jtf.getText());
        jta.append("("+message.getSendTime()+")"+ownId+" 对 "+friendId+" 说:"+jtf.getText()+System.lineSeparator());
        jtf.setText("");
        //发送给服务器
        try{
            OutputStream os = ClientSocketThreadManager.getClientConServerThread(ownId).getS().getOutputStream();
            ObjectOutputStream oos= new ObjectOutputStream(os);
            oos.writeObject(message);
        }catch (Exception arg0){
            arg0.printStackTrace();
        }
    }

    //接收消息函数
    public void showMessage(Message m){
        String info;
        //如果是ALL群发消息
        if(!m.getMesType().equals(MessageType.message_commonToAll))
            info ="("+m.getSendTime()+")"+m.getSender()+" 对 "+m.getGetter()+" 说:"+m.getText()+System.lineSeparator();
        else { //私聊消息
            info ="("+m.getSendTime()+")"+m.getSender()+" 对 "+"All"+" 说:"+m.getText()+System.lineSeparator();
        }
        this.jta.append(info);
    }

    public void receiveFile(Message m){
        String info;
        if(!m.getMesType().equals(MessageType.message_fileToAll))
            info ="("+m.getSendTime()+")"+m.getSender()+" 刚才给 "+m.getGetter()+" 发送了一个文件"+System.lineSeparator();
        else{
            info ="("+m.getSendTime()+")"+m.getSender()+" 刚才给 "+"All"+" 发送了一个文件"+System.lineSeparator();
        }
        this.jta.append(info);
        JFileChooser jf = new JFileChooser();
        jf.setSelectedFile(new File(m.fileName));
        int result=jf.showSaveDialog(this);
        if(result==JFileChooser.APPROVE_OPTION){
            File file = jf.getSelectedFile();
            try {
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(m.data);
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_ENTER){
            sendMessage();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}

//package com.client.view;
//
//import com.client.controller.ClientSocketThreadManager;
//import com.common.Message;
//import com.common.MessageType;
//
//import javax.swing.*;
//import java.awt.*;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
//import java.awt.event.KeyEvent;
//import java.awt.event.KeyListener;
//import java.io.*;
//
///**
// * @author lxl
// * @date 2020/12/26 - 9:38
// */
///*
//与好友聊天的界面
//客户端要处于读取的状态,把他做成线程
// */
//public class ChatFrame extends JFrame implements ActionListener, KeyListener {
////    public static void main(String[] args) {
////        new QqChat("1","2");
////    }
//    JFrame parent;
//    JTextArea jta;      //文本窗口
//    JScrollPane jsp;
//    JTextField jtf;
//    JButton jb1;        //文本消息按钮
//    JButton jb2;        //文件发送按钮
//    JButton jb3;        //聊天记录按钮
//    JButton jb4;        //窗口抖动按钮
//    JPanel jp;
//    String ownId;
//    String friendId;
//    public ChatFrame(JFrame parent, String ownId, String friendId){
//        this.parent=parent;
//        this.ownId=ownId;
//        this.friendId=friendId;
//
//        jta = new JTextArea();
//        jta.setEditable(false);
//        jsp = new JScrollPane(jta);
//        jtf = new JTextField(10);
//        jtf.addKeyListener(this);
//        jb1 = new JButton("发送");
//        jb1.addActionListener(this);
//        jb2 = new JButton("发送文件");
//        jb2.addActionListener(this);
//        jb3 = new JButton("聊天记录");
//        jb3.addActionListener(this);
//        jb4 = new JButton("发送抖动");
//        jb4.addActionListener(this);
//        jp = new JPanel();
//        jp.add(jb2);
//        jp.add(jtf);
//        jp.add(jb1);
//        jp.add(jb3);
//        jp.add(jb4);
//
//        this.setTitle(ownId+" 正在和 "+friendId+" 聊天");
//        this.add(jsp);
//        this.add(jp,"South");
//        this.setSize(800,400);
//        this.setDefaultCloseOperation(HIDE_ON_CLOSE);
//        this.setLocationRelativeTo(parent);
//        this.setVisible(true);
//    }
//
//    @Override
//    public void actionPerformed(ActionEvent e) {
//        if(e.getSource()== jb1){
//            sendMessage();
//        }else if(e.getSource()==jb2){
//            JFileChooser jf = new JFileChooser();
//            jf.setFileSelectionMode(JFileChooser.FILES_ONLY);
//            jf.setCurrentDirectory(new File("."));
//            int result=jf.showOpenDialog(this);
//            if(result==JFileChooser.APPROVE_OPTION){
//                Message message = new Message();
//                if(friendId.equals("All")){//以All结尾代表群发
//                    message.setMesType(MessageType.message_fileToAll);
//                }else{
//                    message.setMesType(MessageType.message_file);
//                }
//                File file = jf.getSelectedFile();
//                message.fileName=file.getName();
//                message.data= new byte[(int) file.length()];
//                try {
//                    FileInputStream fis = new FileInputStream(file);
//                    fis.read(message.data);
//                    fis.close();
//                } catch (FileNotFoundException fileNotFoundException) {
//                    fileNotFoundException.printStackTrace();
//                } catch (IOException ioException) {
//                    ioException.printStackTrace();
//                }
//                message.setSender(ownId);
//                message.setGetter(friendId);
//                message.setText("一个文件");
//                jta.append("("+message.getSendTime()+")"+ownId+" 刚才给 "+friendId+" 发送了一个文件"+System.lineSeparator());
//                try {
//                    OutputStream os = ClientSocketThreadManager.getClientConServerThread(ownId).getS().getOutputStream();
//                    ObjectOutputStream oos= new ObjectOutputStream(os);
//                    oos.writeObject(message);
//                }catch (Exception arg0){
//                    arg0.printStackTrace();
//                }
//            }
//
//        }else if(e.getSource()==jb3){
//            try {
//                new RecordFrame(parent,ownId,friendId);
//            } catch (Exception ex) {
//                ex.printStackTrace();
//            }
//        }else if (e.getSource() == jb4) {
//            Message message = new Message();
//            if (friendId.equals("All"))
//                message.setMesType(MessageType.message_shakeWindowToALL); //抖动窗口类型数据
//            else
//                message.setMesType(MessageType.message_shakeWindow);
//            message.setSender(ownId);                                 //发送者
//            message.setGetter(friendId);                              //接收者
//            message.setText(ownId + "发送了一个窗口抖动!");
//            try {
//                OutputStream os = ClientSocketThreadManager.getClientConServerThread(ownId).getS().getOutputStream();
//                ObjectOutputStream oos= new ObjectOutputStream(os);
//                oos.writeObject(message);
//                System.out.println("你发送了一个抖动窗口");
//            }catch (Exception arg0){
//                arg0.printStackTrace();
//            }
//        }
//    }
//
//    //窗口抖动
//    public void ShakingWindow() {
//        this.setVisible(true);
//        Point p = this.getLocationOnScreen(); //获取原始位置
//        int times = 0;
//        while (times < 2) {
//            try {
//                //进行窗口抖动
//                this.setLocation((int)p.getX(), (int)p.getY() + 75);
//                Thread.sleep(130);
//                this.setLocation((int)p.getX(), (int)p.getY() - 75);
//                Thread.sleep(130);
//                this.setLocation((int)p.getX() + 75, (int)p.getY());
//                Thread.sleep(130);
//                this.setLocation((int)p.getX() - 75, (int)p.getY());
//                Thread.sleep(130);
//                this.setLocation((int)p.getX() - 75, (int)p.getY() - 75);
//                Thread.sleep(130);
//                this.setLocation((int)p.getX() + 75, (int)p.getY() + 75);
//                Thread.sleep(130);
//            } catch (InterruptedException interruptedException) {
//                interruptedException.printStackTrace();
//            }
//            this.setLocation(p); //恢复位置
//            times++;
//        }
//    }
//
//    public void sendMessage(){
//        //点击了发送按钮
//        Message message = new Message();
//        message.setSender(ownId);
//        message.setGetter(friendId);
//        if(friendId.equals("All")){//以All结尾代表群发
//            message.setMesType(MessageType.message_commonToAll);
//        }else{
//            message.setMesType(MessageType.message_common);
//        }
//        message.setText(jtf.getText());
//        jta.append("("+message.getSendTime()+")"+ownId+" 对 "+friendId+" 说:"+jtf.getText()+System.lineSeparator());
//        jtf.setText("");
//        //发送给服务器
//        try{
//            OutputStream os = ClientSocketThreadManager.getClientConServerThread(ownId).getS().getOutputStream();
//            ObjectOutputStream oos= new ObjectOutputStream(os);
//            oos.writeObject(message);
//        }catch (Exception arg0){
//            arg0.printStackTrace();
//        }
//    }
//
//    //接收消息函数
//    public void showMessage(Message m){
//        String info;
//        //如果是ALL群发消息
//        if(!m.getMesType().equals(MessageType.message_commonToAll))
//            info ="("+m.getSendTime()+")"+m.getSender()+" 对 "+m.getGetter()+" 说:"+m.getText()+System.lineSeparator();
//        else { //私聊消息
//            info ="("+m.getSendTime()+")"+m.getSender()+" 对 "+"All"+" 说:"+m.getText()+System.lineSeparator();
//        }
//        this.jta.append(info);
//    }
//
//    public void receiveFile(Message m){
//        String info;
//        if(!m.getMesType().equals(MessageType.message_fileToAll))
//            info ="("+m.getSendTime()+")"+m.getSender()+" 刚才给 "+m.getGetter()+" 发送了一个文件"+System.lineSeparator();
//        else{
//            info ="("+m.getSendTime()+")"+m.getSender()+" 刚才给 "+"All"+" 发送了一个文件"+System.lineSeparator();
//        }
//        this.jta.append(info);
//        JFileChooser jf = new JFileChooser();
//        jf.setSelectedFile(new File(m.fileName));
//        int result=jf.showSaveDialog(this);
//        if(result==JFileChooser.APPROVE_OPTION){
//            File file = jf.getSelectedFile();
//            try {
//                FileOutputStream fos = new FileOutputStream(file);
//                fos.write(m.data);
//                fos.close();
//            } catch (FileNotFoundException e) {
//                e.printStackTrace();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//
//    @Override
//    public void keyTyped(KeyEvent e) {
//
//    }
//
//    @Override
//    public void keyPressed(KeyEvent e) {
//        if(e.getKeyCode()==KeyEvent.VK_ENTER){
//            sendMessage();
//        }
//    }
//
//    @Override
//    public void keyReleased(KeyEvent e) {
//
//    }
//}
